/*
 * jinlanbao
 */
package com.jlb.zx.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.ZxKuangFenException;
import com.jlb.zx.dao.ZxKuangFenDAO;
import com.jlb.zx.entity.ZxKuangFen;
import com.jlb.zx.entity.ZxKuangFenState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("zxKuangFenSV")
@Slf4j
public class ZxKuangFenSVImpl extends BaseSVImpl<ZxKuangFen, Long> implements ZxKuangFenSV {

    @Autowired
    private ZxKuangFenDAO zxKuangFenDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return zxKuangFenDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(ZxKuangFen entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象ZxKuangFen
     * @param id 
     * @return ZxKuangFen
     */
    @Override
    public ZxKuangFen load(Long id) {
           if(id==null){
                 throw new ZxKuangFenException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            return zxKuangFenDAO.load(param);
    }
    /**
     * 加载一个对象ZxKuangFen 通过id
     * @param id 
     * @return ZxKuangFen
     */
    @Override
    public ZxKuangFen loadById(Long id) {
            if(id==null){
                 throw new ZxKuangFenException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return zxKuangFenDAO.loadById(id);
    }


    /**
    * 根据主键id,oldStates 共同更新 ZxKuangFen 的状态到newState状态
    *
    * @param id 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateById(Long id,ZxKuangFenState newState,ZxKuangFenState... oldStates){
        if(id==null){
            throw new ZxKuangFenException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new ZxKuangFenException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new ZxKuangFenException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        zxKuangFenDAO.updateStateById(id,new Date(),newState,oldStates);
    }

    /**
    * 根据主键id 更新 ZxKuangFen 的状态到另一个状态
    *
    * @param id 
    * @param state 状态
    */
    @Override
    public void updateById(Long id,ZxKuangFenState state){
            if(id==null){
               throw new ZxKuangFenException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          zxKuangFenDAO.updateById(id,state,new Date());
    }


    /**
     * 删除对象ZxKuangFen
     * @param id 
     * @return ZxKuangFen
     */
    @Override
    public void delete(Long id) {
            if(id==null){
                throw new ZxKuangFenException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            zxKuangFenDAO.delete(param);
    }



    /**
    * 查询ZxKuangFen分页
    *
    * @param zxKuangFen  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<ZxKuangFen>
    */
    @Override
    public List<ZxKuangFen> list(ZxKuangFen zxKuangFen, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (zxKuangFen != null) {
               map = JSON.parseObject(JSON.toJSONString(zxKuangFen, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return zxKuangFenDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(ZxKuangFen zxKuangFen){
        Map<String, Object> map = null;
        if (zxKuangFen != null) {
           map = JSON.parseObject(JSON.toJSONString(zxKuangFen, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return zxKuangFenDAO.count(map);
     }

    /**
     * 查询ZxKuangFen分页
     *
     * @param id  
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<ZxKuangFen>
     */
    @Override
    public List<ZxKuangFen> list(Long id,int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            return zxKuangFenDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id) {
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            return zxKuangFenDAO.count(param);
    }

    @Override
    public ZxKuangFen loafMaxKuang(String account) {
        return zxKuangFenDAO.loafMaxKuang(account);
    }

}
